/*
 * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 * The software in this package is published under the terms of the CPAL v1.0
 * license, a copy of which has been included with this distribution in the
 * LICENSE.txt file.
 */

import com.jayway.restassured.RestAssured;
import org.junit.Rule;
import org.junit.Test;
import org.mule.tck.junit4.FunctionalTestCase;
import org.mule.tck.junit4.rule.DynamicPort;
import org.mule.util.IOUtils;

import java.io.InputStream;

import static com.jayway.restassured.RestAssured.given;
import static org.hamcrest.core.Is.is;

public class BOMTestCase extends FunctionalTestCase
{

    @Rule
    public DynamicPort serverPort = new DynamicPort("http.port");

    @Override
    public int getTestTimeoutSecs()
    {
        return 6000;
    }

    @Override
    protected void doSetUp() throws Exception
    {
        RestAssured.port = serverPort.getNumber();
        super.doSetUp();
    }

    @Override
    protected String getConfigResources()
    {
        return "org/mule/module/apikit/unicode/bom-test/bom-test-config.xml";
    }

    @Test
    public void trimBomInXMLPayload()
    {
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("org/mule/module/apikit/unicode/bom-test/api/examples/person-example.xml");
        byte[] body = IOUtils.toByteArray(inputStream);

        given()
                .contentType("application/xml")
                .body(body)
                .expect()
                    .statusCode(200)
                    .body(is("{\"status\": \"ok\"}"))
                .when()
                    .post("/api/target");
    }
}
